﻿using System.Security.Cryptography;
using System.Text;
using System.Windows;

namespace WpfApp4
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void GenerateStringButton_Click(object sender, RoutedEventArgs e)
        {
            string s = "";

            string s1 = this.RichTextBox1.Text;
            string s2 = s1.Replace("\\", "\\\\");
            s2 = s2.Replace("\"", "\\\"");
            char[] split = new char[] { '\n' };
            string[] temp = s2.Split(split);

            if (this.ThemeComboBox.Text == "string_builder")
            {
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().Length > 2)
                    {
                        if (!temp[i].Trim().Substring(0, 2).Equals("//"))
                        {
                            s += "strTrans.Append(\"" + temp[i] + "\\n\");\n";
                        }
                        else
                        {
                            s += temp[i] + "\n";
                        }
                    }
                    else
                    {
                        s += "strTrans.Append(\"" + temp[i] + "\\n\");\n";
                    }
                }
            }
            else if (this.ThemeComboBox.Text == "Command")
            {

                string strTrans = "";
                strTrans += "Command=\"{Binding NavigateCommand}\"\n";
                strTrans += "\n";

                strTrans += " public ICommand NavigateCommand\n";
                strTrans += " {\n";
                strTrans += "  get\n";
                strTrans += "  {\n";
                strTrans += "   return new DelegateCommand<TreeNodeConfig>(Navigate);\n";
                strTrans += "  }\n";
                strTrans += " }\n";
                strTrans += "  private void Navigate(TreeNodeConfig selectedItem)\n";
                strTrans += "  {\n";
                strTrans += "  }\n";

                s += strTrans;
            }

            else if (this.ThemeComboBox.Text == "属性")
            {

                string strTrans = "";
                strTrans += "  private bool _" + s1 + ";\n";
                strTrans += "  public bool " + s1 + "\n";
                strTrans += "  {\n";
                strTrans += "      get { return _" + s1 + "; }\n";
                strTrans += "      set\n";
                strTrans += "      {\n";
                strTrans += "          _" + s1 + " = value;\n";
                strTrans += "          RaisePropertyChanged(nameof(" + s1 + "));\n";
                strTrans += "       }\n";
                strTrans += "  }\n";

                s += strTrans;
            }
            else if (this.ThemeComboBox.Text == "+")
            {
                s += "string strTrans=\"\";\n";
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().Length > 2)
                    {
                        if (!temp[i].Trim().Substring(0, 2).Equals("//"))
                        {
                            s += "strTrans+=\"" + temp[i].Trim('\n').Trim('\r') + "\\n\";\n";
                        }
                        else
                        {
                            s += temp[i].Trim('\n').Trim('\r') + "\n";
                        }
                    }
                    else
                    {
                        s += "strTrans+=\"" + temp[i].Trim('\n').Trim('\r') + "\\n\";\n";
                    }
                }
            }
            else if (this.ThemeComboBox.Text == "MD5Hash")
            {
                s = MD5Hash(s2);
            }

            this.RichTextBox2.Text = s;
        }

        public static string MD5Hash(string text)
        {
            MD5 md5 = MD5.Create(); //new MD5CryptoServiceProvider();

            //compute hash from the bytes of text  
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it  
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits  
                //for each byte  
                strBuilder.Append(result[i].ToString("X2"));
            }

            return strBuilder.ToString();
        }
    }
}